<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习</title>
    <link rel="stylesheet" href="../../css/common.css">
    <style>
        .box {
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="save">
        保存
    </div>
    <script src='../../js/common.js'></script>
    <script>
        function fn () {
            console.log('ok')
        }

        // resize 结束1s之后再执行函数
        function debounce (fn, delay) {
            let timer = null
            return function () {
                if (timer) {
                    clearTimeout(timer)
                    timer = setTimeout(fn, delay)
                } else {
                    timer = setTimeout(fn, delay)
                }
            }
        }

        window.onresize = debounce(fn, 1000)

        // 1s钟之内不管怎么点击按钮 都只会触发一次 
        function throttle (fn, delay) {
            let valid = true
            return function () {
                if (!valid) {
                    return false
                }
                valid = false
                fn()
                setTimeout(() => {
                    valid = true
                }, delay)
            }
        }

        $('#save').click(throttle(fn, 2000))

    </script>
</body>

</html>